package com.jxt.myjxtmanager.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.MainActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.utils.ToastUtil;

import java.util.Timer;
import java.util.TimerTask;

import cn.jpush.sms.SMSSDK;
import cn.jpush.sms.listener.SmscheckListener;
import cn.jpush.sms.listener.SmscodeListener;

/**
 * 忘记密码类
 * Created by qm on 2017/2/9 0009.
 */
public class ForgetPswActivity extends BaseActivity {

    private TextInputEditText edit_forget_username;
    private TextInputEditText edit_forget_password;
    private TextInputEditText edit_verify_code;
    private Button btn_get_verify_code;
    private AppCompatButton ibtn_login;
    private String verifyCode;
    private String phoneNum;

    private TimerTask timerTask;
    private Timer timer;
    private int timess;
    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_psw;
    }

    @Override
    protected void initView() {
        setToolbarTitle("忘记密码");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        edit_forget_username = (TextInputEditText) findViewById(R.id.edit_forget_username);
        edit_forget_password = (TextInputEditText) findViewById(R.id.edit_forget_password);
        edit_verify_code = (TextInputEditText) findViewById(R.id.edit_verify_code);
        btn_get_verify_code = (Button) findViewById(R.id.btn_get_verify_code);
        ibtn_login = (AppCompatButton) findViewById(R.id.ibtn_login);
        btn_get_verify_code.setOnClickListener(this);
        ibtn_login.setOnClickListener(this);
        progressDialog = new ProgressDialog(this);
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_verify_code:
                if (virfyPhone()) {
                    btn_get_verify_code.setClickable(false);
                    //开始倒计时
                    startTimer();
                    getVerifyCode();
                }
                break;
            case R.id.ibtn_login:
                changePsw();
                break;
        }
    }

    /**
     * 获取验证码信息  通过短信
     */
    private void getVerifyCode() {
        phoneNum = edit_forget_username.getText().toString().trim();
        SMSSDK.getInstance().getVoiceCodeAsyn(phoneNum, new SmscodeListener() {
            @Override
            public void getCodeSuccess(final String uuid) {
//                ToastUtil.showToast(this, uuid);
                edit_verify_code.setText(uuid);
                verifyCode = uuid;
            }

            @Override
            public void getCodeFail(int errCode, final String errmsg) {
                //失败后停止计时
                stopTimer();
                ToastUtil.showToast(ForgetPswActivity.this, errmsg);
            }
        });
    }

    /**
     * 验证手机信息输入与否  true 已输入  false 为空
     *
     * @return
     */
    private boolean virfyPhone() {
        phoneNum = edit_forget_username.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.showToast(this, "请输入手机号码");
            return false;
        } else
            return true;
    }

    /**
     * 修改密码核心操作
     */
    private void changePsw() {

        String code = edit_verify_code.getText().toString().trim();
        String phoneNum = edit_forget_username.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showToast(ForgetPswActivity.this, "请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.showToast(ForgetPswActivity.this, "请输入手机号码");
            return;
        }
        progressDialog.setTitle("正在验证...");
        progressDialog.show();
        SMSSDK.getInstance().checkSmsCodeAsyn(phoneNum, code, new SmscheckListener() {
            @Override
            public void checkCodeSuccess(final String code) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                SaleUser saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
                if (saleUser == null) {
                    saleUser = new SaleUser(System.currentTimeMillis(), System.currentTimeMillis() + "", "昵称", edit_forget_username.getText().toString().trim(), "", edit_forget_password.getText().toString().trim());
                } else {
                    saleUser.setPassword(edit_forget_password.getText().toString().trim());
                    saleUser.setPhone(edit_forget_username.getText().toString().trim());
                }
                SaleUserModel.getInstance().insertOrReplace(saleUser);
//                ToastUtil.showToast(ForgetPswActivity.this, code);

                startActivity(new Intent(ForgetPswActivity.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
                onBackPressed();
            }

            @Override
            public void checkCodeFail(int errCode, final String errmsg) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                ToastUtil.showToast(ForgetPswActivity.this, errmsg);
            }
        });
    }

    /**
     * 获取验证码时间计时器  开始
     */
    private void startTimer() {
        timess = (int) (SMSSDK.getInstance().getIntervalTime() / 1000);
        btn_get_verify_code.setText(timess + "s");
        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            timess--;
                            if (timess <= 0) {
                                stopTimer();
                                return;
                            }
                            btn_get_verify_code.setText(timess + "s");
                        }
                    });
                }
            };
        }
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(timerTask, 1000, 1000);
    }

    /**
     * 获取验证码时间计时器  结束
     */
    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        btn_get_verify_code.setText("重新获取");
        btn_get_verify_code.setClickable(true);
    }
}
